/**
 * 
 */
package com.munoor.dalai.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTree;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.gwt.view.client.TreeViewModel;
import com.munoor.dalai.client.presenter.ControlPanelPresenter;


/**
 * @author Osho
 * 
 */
public class ControlPanel extends Composite implements ControlPanelPresenter.Display {
	//private ControlPanelPresenter presenter = null;
	private static ControlPanelUiBinder uiBinder = GWT.create(ControlPanelUiBinder.class);
	private MenuTreeModel treeModel;
	/*
	 * @UiField(provided=true) CellTree controlMenuTree = new CellTree( new
	 * TreeViewModel() { final AbstractDataProvider<String> dataProvider = new
	 * ListDataProvider<String>(); final AbstractSelectionModel<String>
	 * selectionModel = new NoSelectionModel<String>();
	 * 
	 * @Override public <T> NodeInfo<?> getNodeInfo(T value) { return new
	 * DefaultNodeInfo<String>(dataProvider, new TextCell(), selectionModel,
	 * null); }
	 * 
	 * @Override public boolean isLeaf(Object value) { return true; } }, null);
	 */
	@UiField(provided = true)
	CellTree controlMenuTree;

	interface ControlPanelUiBinder extends UiBinder<Widget, ControlPanel> {
	}

	public void setPresenter(ControlPanelPresenter p) {
		//treeModel.setEventProcessor(p);
	}	
	
	public ControlPanel() {
		MenuTreeModel treeModel = new MenuTreeModel();
		controlMenuTree = new CellTree(treeModel, null);
		initWidget(uiBinder.createAndBindUi(this));
	}

	private static class MenuCategory {
		private final String name;
		private final List<String> subMenuList = new ArrayList<String>();

		public MenuCategory(String name) {
			this.name = name;
		}

		public String getName() {
			return name;
		}

	    public void addSubMenu(String name) {
	    	subMenuList.add(name);
	    }
	    
		/**
		 * Return the list of songs in the playlist.
		 */
		public List<String> getSubMenu() {
			return subMenuList;
		}
	}

	private static class MenuTreeModel implements TreeViewModel {
		private final List<MenuCategory> menuCategories;
		private final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>();
		
		public MenuTreeModel() {
			menuCategories =  new ArrayList<MenuCategory>();
			MenuCategory menuItem = new MenuCategory("Listings");
			
			menuItem.addSubMenu("List");
			menuItem.addSubMenu("Filter");
			menuCategories.add(menuItem);
			
			menuItem = new MenuCategory("Settings");
			menuItem.addSubMenu("Settings-A");
			menuItem.addSubMenu("Settings-B");
			menuCategories.add(menuItem);
		}

		public void setEventProcessor(final ControlPanelPresenter p){
			 selectionModel.addSelectionChangeHandler(
			            new SelectionChangeEvent.Handler() {
			              public void onSelectionChange(SelectionChangeEvent event) {
			            	  p.fireSelectionChange();
			              }
			            });
		}
		@Override
		public <T> NodeInfo<?> getNodeInfo(T value) {
			 if (value == null) {
			        // LEVEL 0.
			        // We passed null as the root value. Return the composers.

			        // Create a data provider that contains the list of composers.
			        ListDataProvider<MenuCategory> dataProvider = new ListDataProvider<ControlPanel.MenuCategory>(menuCategories);

			        // Create a cell to display a composer.
			        Cell<MenuCategory> cell = new AbstractCell<MenuCategory>() {
			          @Override
			          public void render(Context context, MenuCategory value, SafeHtmlBuilder sb) {
			            if (value != null) {
			              sb.appendEscaped(value.getName());
			            }
			          }
			        };

			        // Return a node info that pairs the data provider and the cell.
			        return new DefaultNodeInfo<MenuCategory>(dataProvider, cell);
			      } else if (value instanceof MenuCategory) {
			        // LEVEL 1.
			        // We want the children of the MenuCategory. Return the SubMenus.
			        ListDataProvider<String> dataProvider = new ListDataProvider<String>(
			            ((MenuCategory) value).getSubMenu());
			        return new DefaultNodeInfo<String>(dataProvider, new TextCell(),selectionModel, null);
			      } 
			      return null;
		}

		@Override
		public boolean isLeaf(Object value) {
		      if (value instanceof String) {
		          return true;
		        }
		        return false;
		}
		
	}
	
}
